<?php

class ectools_mdl_regions extends dbeav_model {

    public function get_package_info() {
        return kernel::service('ectools_regions.ectools_mdl_regions');
    }

    public function getById($regionId = '') {
        // return $this->db->selectrow("select local_name from ".$this->table_name(1)." where region_id=".intval($regionId));
        return $this->dump(intval($regionId), 'local_name');
    }

    public function getRegionByParentId($parentId) {
        return $this->dump(intval($parentId), 'region_id,local_name,p_region_id');
    }

    public function getAllChild($regionId) {
        unset($this->IdGroup);
        
        $tmpRow = $this->dump(intval($regionId), 'region_path');
        $sql = "select region_id from " . $this->table_name(1) . " where region_path like '%" . $tmpRow['region_path'] . "%'";
        $row = $this->db->select($sql);
        
        if (is_array($row) && count($row) > 0) {
            foreach ($row as $key => $val) {
                $this->IdGroup[] = $val['region_id'];
            }
        }
        
        return $this->IdGroup;
    }

    public function getGroupRegionId($regionId) {
        // $row = $this->db->selectrow($sql='select region_path from '.$this->table_name(1).' where region_id='.intval($regionId));
        $row = $this->dump(intval($regionId), 'region_path');
        $path = $row['region_path'];
        $idGroup = array();
        
        $rows = $this->db->select($sql = "select region_id from " . $this->table_name(1) . " where region_path like '%" . $path . "%' and region_id<>" . intval($regionId));
        if ($rows) {
            foreach ($rows as $key => $val) {
                $idGroup[] = $val['region_id'];
            }
        }
        
        return $idGroup;
    }

    public function getDlAreaById($aRegionId) {
        $sql = 'select c.region_id,c.local_name,c.p_region_id,c.ordernum,p.local_name as parent_name from ' . $this->table_name(1) . ' as c LEFT JOIN ' . $this->table_name(1) . ' as p ON p.region_id=c.p_region_id where c.region_id=' . intval($aRegionId);
        
        return $this->db->selectrow($sql);
    }

    public function checkDlArea($name, $p_region_id) {
        if ($p_region_id) {
            $aTemp = $this->dump(array(
                'local_name' => $name,
                'p_region_id' => $p_region_id
            ), 'region_id');
        } else {
            $aTemp = $this->dump(array(
                'local_name' => $name
            ), 'region_id');
        }
        // $aTemp = $this->db->selectrow("SELECT region_id FROM ".$this->table_name(1)." WHERE local_name='".$name."' and p_region_id".($p_region_id?('='.intval($p_region_id)):' is null'));
        return $aTemp['region_id'];
    }

    function is_installed() {
        // $row = $this->db->selectrow('select count(*) as c from '.$this->table_name(1));
        // return $row['c']>0;
        $row = $this->count();
        return $row;
    }

    public function clearOldData($package = '') {
        if ($package)
            $sql = "delete from " . $this->table_name(1) . " where package='" . $package . "'";
        else
            $sql = "delete from " . $this->table_name(1) . " where 1";
        
        $this->db->exec($sql);
    }

    public function change_regions_data($ship_area = '') {
        if (! $ship_area)
            return '';
        
        list ($package, $region_name, $region_id) = explode(':', $ship_area);
        $arr_region_name = explode("/", $region_name);
        $arr_directory_name = array(
            app::get('ectools')->_('北京'),
            app::get('ectools')->_('天津'),
            app::get('ectools')->_('上海'),
            app::get('ectools')->_('重庆')
        );
        
        if (! in_array($arr_region_name[0], $arr_directory_name)) {
            return implode('', $arr_region_name);
            // return $arr_region_name[0].$arr_region_name[1].$arr_region_name[2];
        } else {
            unset($arr_region_name[0]);
            return implode('', $arr_region_name);
            // return $arr_region_name[1].$arr_region_name[2];
        }
    }

    function getRegionInfo($rows = '*', $filter = array()) {
        $whereStr = " 1=1 ";
        if ($filter) {
            foreach ($filter as $key => $val) {
                if (! $val)
                    continue;
                if (is_array($val)) {
                    $where[] = $key . " in ('" . implode("','", $val) . "')";
                } else {
                    $where[] = $key . "=" . $val;
                }
            }
            $whereStr .= " AND " . implode(' AND ', $where);
        }
        return $this->db->select("SELECT " . $rows . " FROM " . DB_PREFIX . "ectools_regions WHERE " . $whereStr);
    }

    function getByRegstrName($names) {
        $parent_id = 0;
        foreach ($names as $k => $name) {
            if ($k == 'province') {
                $pro = $this->db->selectrow("select region_id,local_name,region_path from " . DB_PREFIX . "ectools_regions where local_name='" . trim($name) . "' and region_grade = 1 ");
                $parent_id = $pro['region_id'];
            } elseif ($k == 'city') {
                $city = $this->db->selectrow("select region_id,local_name,region_path from " . DB_PREFIX . "ectools_regions where local_name='" . trim($name) . "' and p_region_id = '$parent_id' ");
                $parent_id = $city['region_id'];
                if ($names['county'] == $name) {
                    return $city;
                    break;
                }
            } elseif ($k == 'county') {
                $country = $this->db->selectrow("select region_id,local_name,region_path from " . DB_PREFIX . "ectools_regions where local_name='" . trim($name) . "' and p_region_id = '$parent_id' ");
            }
        }
        return $country;
    }
}
